// Two function need to be implemented , a2t_handler & a2t_handler_configMode
// used GM Variables
// ds_huntRuleCurrent : current hunt rule object .

function a2t_handler(){
    if(GM_safeVariables["ds_huntRuleCurrent"] != null && GM_safeVariables["ds_huntRuleCurrent"] != ''){
          
        var currentHuntRule = GM_safeVariables["ds_huntRuleCurrent"];
        currentHuntRule = eval('('+currentHuntRule+')');
        if(currentHuntRule.isPointing) {
            var form1 = $("form[action*='/a2t']");
            if(form1.length != 0) {
                var filterstr = "h3:contains('adgsdgasdgasdgfasga')";
                for(var i = 0; i < currentHuntRule.heros.length ; i++) {
                    filterstr = filterstr + ",h3:contains('"+currentHuntRule.heros[i].name+"')"
                }
                var lis = $("ul>li",form1).filter(function(index) {
                    return $(filterstr, this).length != 0;
                });
                var inputA = $("input",lis);
                for(var i=0;i<inputA.length;i++){
                    inputA[i].checked = true;
                }
                if(currentHuntRule.attackType == 'attack') {
                    var inputB = $("input#at4",form1);
                    inputB[0].checked = true;
                }else{
                    var inputB = $("input#at5",form1);
                    inputB[0].checked = true;
                }
                //form1[0].submit();
                var inputS = $("input[name*=save]",form1).click();
            }
            var form2 = $("form#s2t");
            if(form2.length !=0) {
                $('#s2t').get(0).action='/s2t';
                var randomTime = Math.random()*15000;
                randomTime = randomTime<3000?3000:randomTime;
                window.setTimeout(function(){
                    delete GM_safeVariables["ds_huntRuleCurrent"];
                    $('#fbbtn').get(0).click();
                },randomTime);
            }
        }else{
            if(window.location.search == ""){
                if($x('//div[@class="attack"]/p[@class="msg"]').length > 0 
                && $x('//div[@class="attack"]/p[@class="msg"]')[0].innerHTML != ""){
                    log($x('//div[@class="attack"]/p[@class="msg"]')[0].innerHTML + "，跳转回首页重新开始所有流程.");
                    clickLink("/mindex");
                    return;
                }
                //direct to submit the last step for attack
                var btn = $("#fbsb2").get(0);
                if($('#s2t').get(0) == null){
                    log("当前狩猎规则存在错误,请进入军事指挥所纠正该错误，自动跳转回首页");
                    clickLink('/mindex');
                    return;
                }
                $('#s2t').get(0).action='/s2t';
                var randomTime = Math.random()*15000;
                randomTime = randomTime<3000?3000:randomTime;
                window.setTimeout(function(){
                    delete GM_safeVariables["ds_huntRuleCurrent"];
                    $('#fbbtn').get(0).click();//auto transition to the start state,server will redirect the page to mindex page
                },randomTime);
                return;
            }
            //first phase ,selected the hero.
            var currentHuntRule = GM_safeVariables["ds_huntRuleCurrent"];
            currentHuntRule = eval('('+currentHuntRule+')');
            for(var i=0;i<currentHuntRule.heros.length;i++){
                var hero = currentHuntRule.heros[i];    
                $('#armys > li > h3:contains("'+hero.name+'")').siblings(':checkbox[name="heroid"]').get(0).checked = true;
            }
            var strHeroName = '';
            for(var m=0;m<currentHuntRule.heros.length;m++){
                var objHero = currentHuntRule.heros[m];
                strHeroName += objHero.name + ',';
            }
            if($(':checkbox:checked[name="heroid"]').length == 0){
                log("无法选中指定规则中包含的英雄'"+strHeroName+"',自动跳转到首页...");
                clickLink('/mindex');
                return;
            }
            var randomTime = Math.random()*15000;
            randomTime = randomTime<3000?3000:randomTime;
            window.setTimeout(function(){
                //transition to next state
                //感谢小管的发现 这里如果直接使用form提交 http包会导致少了 save=确定的尾巴 改为使用按钮点击提交
                //$x("//form[@action='/a2t']")[0].submit();
                var inputS = $("form>div>p>input[name*=save]").click();
            },randomTime);
            return;
            log("没有可以出征的英雄，跳转到首页...");
            clickLink("/mindex");
        }
    }else{
        log("您没有制定任何的狩猎规则,将自动跳转回首页...");
        clickLink('/mindex');
        return;
    }
}

function a2t_handler_configMode(){
}